package encaps.utils.rendering;

import encaps.resources.Response;
import encaps.utils.StringUtil;
import static encaps.utils.html.HTML.CLASS;
import static encaps.utils.html.HTML.SPAN;
import encaps.utils.html.HTMLElement;
import encaps.utils.html.Styles;
import org.jdom.Element;

public class RadioControlFactory implements FieldFactory {

    @Override
    public HTMLElement renderField(Element e, boolean edit) {
        String key = e.getChildTextTrim(Response.KEY);
        String value = e.getChildTextTrim(Response.VALUE);
        String label = e.getChildTextTrim(Response.LABEL);
        String message = e.getChildTextTrim(Response.MESSAGE);
        HTMLElement p = new HTMLElement(SPAN);
        HTMLElement radioControl = HTMLElement.createRadioButton(key, value, "");
        p.addContent(radioControl);
        if (label != null) {
            p.addContent(HTMLElement.createB(label));
        }
        p.addContent(renderMessage(message));
        return p;
    }

    protected HTMLElement renderMessage(String message) {
        if (!StringUtil.isEmpty(message)) {
            HTMLElement span = new HTMLElement(SPAN);
            span.addAttribute(CLASS, Styles.VALIDATION_MESSAGE);
            span.addContent(" " + message);
            return span;
        } else {
            return null;
        }
    }
}
